package cn.wd.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.wd.dao.CustomerMapper;
import cn.wd.model.Customer;
import cn.wd.model.dto.GoodsDTO;
import cn.wd.service.CustomerService;

@Service
public class CustomerServiceImpl implements CustomerService{
	@Autowired
	CustomerMapper customerMapper;
	@Override
	public void createOrUpdate(GoodsDTO goodsDTO) {
		Customer customer = new Customer();
		customer.setCustomerAddr(goodsDTO.getCustomerAddr());
		customer.setCustomerName(goodsDTO.getCustomerName());
		customer.setCustomerPhone(goodsDTO.getCustomerPhone());
		Customer queryResult = customerMapper.queryByPhone(goodsDTO.getCustomerPhone());
		if(queryResult!=null){
			goodsDTO.setCustomerId(queryResult.getCustomerId()+"");
			customerMapper.updateByPhone(goodsDTO);
		}else if(StringUtils.isNotEmpty(goodsDTO.getCustomerId())){
			//修改
			customer.setCustomerId(new Integer(goodsDTO.getCustomerId()));
			customerMapper.updateByPrimaryKeySelective(customer);
		}else{
			//新增
			goodsDTO.setCustomerId(customerMapper.insertSelective(customer)+"");
		}
	}
	@Override
	public List<Customer> ajaxLoad(String key) {
		List<Customer> customerList = customerMapper.selectByKey(key);
		return customerList;
	}
}
